package com.hcs.chenyu.moreaboutstudent;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.hcs.chenyu.R;

public class addstudent extends AppCompatActivity implements View.OnClickListener{
    private Button mSubmitSTudent;
    private TextView mstuNoText;
    private TextView mstuNameText;
    private TextView mstusexText;
    private TextView mdepartmentText;
    private TextView mclassesText;
    private TextView mtelText;
    private TextView mdormNoText;
    private Button mbtn_ToMain3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addstudent);
        initview();
    }

    private void initview() {
        mSubmitSTudent = findViewById(R.id.SubmitSTudent);
        mstuNoText = findViewById(R.id.stuNoText);
        mstuNameText = findViewById(R.id.stuNameText);
        mstusexText = findViewById(R.id.stusexText);
        mdepartmentText = findViewById(R.id.departmentText);
        mclassesText = findViewById(R.id.classesText);
        mtelText = findViewById(R.id.telText);
        mdormNoText = findViewById(R.id.dormNoText);
        mbtn_ToMain3 = findViewById(R.id.btn_ToMain3);
        mSubmitSTudent.setOnClickListener(this);
        mbtn_ToMain3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(addstudent.this, more.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        //当单击“添加”按钮时，获取添加信息
        String mstudentid=mstuNoText.getText().toString().trim();
        String mstudentname = mstuNameText.getText().toString().trim();
        String mstudentsex = mstusexText.getText().toString().trim();
        String mstudentmajoy = mdepartmentText.getText().toString().trim();
        String mstudentclass = mclassesText.getText().toString();
        String mstudenttel = mtelText.getText().toString();
        String mstudentdormNo = mdormNoText.getText().toString();

        //检验信息是否正确
        if (TextUtils.isEmpty(mstudentid)) {
            Toast.makeText(this, "请输入学号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mstudentname)) {
            Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mstudentsex)) {
            Toast.makeText(this, "请输入性别", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mstudentmajoy)) {
            Toast.makeText(this, "请输入专业", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mstudentclass)) {
            Toast.makeText(this, "请输入班级", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mstudenttel)) {
            Toast.makeText(this, "请输入电话", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(mstudentdormNo)) {
            Toast.makeText(this, "请输入宿舍", Toast.LENGTH_SHORT).show();
            return;
        }
        //添加学生个人信息
        Student s =new Student();
        s.mstudentid= mstudentid;
        s.mstudentname = mstudentname;
        s.mstudentsex = mstudentsex;
        s.mstudentmajoy = mstudentmajoy;
        s.mstudentclass= mstudentclass;
        s.mstudenttel = mstudenttel;
        s.mstudentdormNo =mstudentdormNo;
        //创建数据库访问对象
        Studentdao da = new Studentdao(getApplicationContext());
        //打开数据库
        da.open();
        //执行数据库访问方法
        long result = da.addstudent(s);

        if (result > 0) {
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
        }
        //关闭数据库
        da.close();
        //关闭活动
        finish();
    }
}